package com.sw.haruka.module.music;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sw.haruka.R;
import com.sw.haruka.module.base.BaseFragment;
import com.sw.haruka.view.MainActivity;

import java.io.File;

/**
 * Created by shqyang on 05/30/2021
 */
public class MusicFragment extends BaseFragment implements MusicManager.Observer {

    private TextView mNameTv;
    private ImageView mStartAndPauseIv;
    private ImageView mStopIv;
    private ImageView mLoopIv;

    private File mMusicFile;
    public MusicFragment(File file) {
        mMusicFile = file;
        MusicManager.getInstance().addObserver(this);
    }

    @Override
    protected int layoutId() {
        return R.layout.music_fragment;
    }

    @Override
    protected void initView() {
        mNameTv = findViewById(R.id.nameAndArtist_tv);
        mStartAndPauseIv = findViewById(R.id.startAndPause_iv);
        mStopIv = findViewById(R.id.stop_iv);
        mLoopIv = findViewById(R.id.loop_iv);
        setCover();
    }

    private void setCover() {
        Bitmap cover = MusicManager.getInstance().getCover(mMusicFile.getPath());
        if (cover != null) {
            ImageView coverIv = findViewById(R.id.cover_iv);
            coverIv.setImageBitmap(cover);
        }
    }



    @Override
    protected void initEvent() {
        mStopIv.setOnClickListener(v -> {
            MusicManager.getInstance().stop();
        });
        mStartAndPauseIv.setOnClickListener(v -> {
            if (MusicManager.getInstance().getPlayerState() == MusicServer.State.PLAY) {
                // playing
                MusicManager.getInstance().pause();
            } else {
                MusicManager.getInstance().start();
            }
        });
        findViewById(R.id.close_iv).setOnClickListener(v -> {
            ((MainActivity)mActivity).exitFragment();
        });
        findViewById(R.id.loop_iv).setOnClickListener(v -> {
            MusicManager.getInstance()
                    .switchLoop();
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void loadData(boolean isRefresh) {
        String path = mMusicFile.getPath();
        MusicManager.getInstance().setSource(path);
        mNameTv.setText(mMusicFile.getName());
        Music music = MusicManager.getInstance().getInfo(path);
        mNameTv.setText(music.getName() + " - " + music.getArtist());
    }

    private void refreshUI(MusicServer.State state, boolean isLoop) {
        mLoopIv.setImageResource(isLoop ? R.drawable.ic_loop : R.drawable.ic_not_loop);
        switch (state) {
            case PLAY:
                mStartAndPauseIv.setImageResource(R.drawable.ic_pause);
                mStopIv.setVisibility(View.VISIBLE);
                break;
            case PAUSE:
                mStartAndPauseIv.setImageResource(R.drawable.ic_play);
                mStopIv.setVisibility(View.VISIBLE);
                break;
            case STOP:
                mStartAndPauseIv.setImageResource(R.drawable.ic_play);
                mStopIv.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void notifyState(MusicServer.State state, boolean isLoop) {
        refreshUI(state, isLoop);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MusicManager.getInstance().removeObserver(this);
    }
}
